package utils

import (
	"strings"

	"mall/common/globalkey"
	"mall/service/backend/common/enum/common"
)

type Option struct {
	CanAccept  bool
	CanReject  bool
	CanRemove  bool
	CanRestore bool
	CanSubmit  bool
	CanStick   bool
}

func GetOptionByStatus(status *uint32) *Option { //##24.11.20##
	res := &Option{}

	if status != nil {
		switch int32(*status) {
		case globalkey.StatusDisabled:
			res.CanAccept = true
			res.CanRemove = true
		case globalkey.StatusEnabled:
			res.CanReject = true
			res.CanRemove = true
		case globalkey.StatusDeleted:
			res.CanRestore = true
		}
	}

	return res
}

func GetOptionByStatusAndRoleIds(status *uint32, roleIds []uint64) *Option {
	res := &Option{}

	for _, id := range roleIds {
		if id <= common.DefaultAdminRoleId { // 是默认的系统管理员或超级管理员角色
			return res
		}
	}

	if status != nil {
		switch int32(*status) {
		case globalkey.StatusDisabled:
			res.CanAccept = true
			res.CanRemove = true
		case globalkey.StatusEnabled:
			res.CanReject = true
			res.CanRemove = true
		}
	}

	return res
}

func GetAuditOptionByStatus(status *uint32) *Option { //##24.11.20##
	res := &Option{}

	if status != nil {
		switch int32(*status) {
		case globalkey.StatusPending:
			res.CanAccept = true
			res.CanReject = true
			res.CanRemove = true
		case globalkey.StatusRejected:
			res.CanRemove = true
		case globalkey.StatusDeleted:
			res.CanRestore = true
		case globalkey.StatusDraft:
			res.CanSubmit = true
			res.CanRemove = true
		case globalkey.StatusApproved:
			res.CanStick = true
		}
	}

	return res
}

func ParseMetaData(metaData *string) (filename string, mimeType string, newMetaData string) { // ##24.11.22##
	if metaData == nil {
		return
	}

	if len(strings.TrimSpace(*metaData)) == 0 {
		return
	}

	// "filename:82Wf2sg8gM.jpg;size:61865;md5:;mimeType:image/jpeg"
	items := strings.Split(strings.TrimSpace(*metaData), ";")
	if len(items) == 0 {
		return
	}

	var newItems []string
	for _, item := range items {
		parts := strings.Split(item, ":")

		if len(parts) == 2 && len(strings.TrimSpace(parts[0])) > 0 && len(strings.TrimSpace(parts[1])) > 0 {
			switch strings.TrimSpace(parts[0]) {
			case "filename":
				filename = strings.TrimSpace(parts[1])
			case "mimeType":
				mimeType = strings.TrimSpace(parts[1])
			default:
				newItems = append(newItems, item)
			}
		}
	}

	return filename, mimeType, strings.Join(newItems, ";")
}
